type Param = "a" | "n";

export class Cache {
	static _ins: Cache;

	static url_more_a = "/asset/imgs/c_more_a.png";
	static url_more_n = "/asset/imgs/c_more_n.png";
	static url_b_a = "/asset/imgs/b_a.png";
	static url_b_n = "/asset/imgs/b_n.png";
	static url_s_a = "/asset/imgs/s_a.png";
	static url_s_n = "/asset/imgs/s_n.png";
	static url_d_a = "/asset/imgs/d_a.png";
	static url_d_n = "/asset/imgs/d_n.png";
	static url_w_a = "/asset/imgs/w_a.png";
	static url_w_n = "/asset/imgs/w_n.png";
	static url_t_n = "/asset/imgs/t_n.png";
	static url_news_a = "/asset/imgs/news.png";

	map: Map<string, any>;

	constructor() {
		this.map = new Map();
	}

	async getMore(type: Param) {
		let url = type == "a" ? Cache.url_more_a : Cache.url_more_n;
		let img = await this.loadImg(url);
		return img;
	}

	async getCharB(type: Param) {
		let url = type == "a" ? Cache.url_b_a : Cache.url_b_n;
		let img = await this.loadImg(url);
		return img;
	}

	async getCharS(type: Param) {
		let url = type == "a" ? Cache.url_s_a : Cache.url_s_n;
		let img = await this.loadImg(url);
		return img;
	}

	async getCharD(type: Param) {
		let url = type == "a" ? Cache.url_d_a : Cache.url_d_n;
		let img = await this.loadImg(url);
		return img;
	}

	async getCharW(type: Param) {
		let url = type == "a" ? Cache.url_w_a : Cache.url_w_n;
		let img = await this.loadImg(url);
		return img;
	}
	async getCharT(type: Param) {
		let url = Cache.url_t_n;
		let img = await this.loadImg(url);
		return img;
	}
	async getNews() {
		let url = Cache.url_news_a;
		let img = await this.loadImg(url);
		return img;
	}
	loadImg(url: string) {
		return new Promise(resolve => {
			let temp = this.map.get(url);
			if (temp) {
				resolve(temp);
			} else {
				let img = new Image();
				img.onload = () => {
					this.map.set(url, img);
					resolve(img);
				};
				img.src = url;
			}
		});
	}

	static get ins() {
		if (!Cache._ins) {
			Cache._ins = new Cache();
		}
		return Cache._ins;
	}
}
